package ywpt.web.action.system;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import net.sf.json.util.WebUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import ywpt.components.acegi.domain.Function;
import ywpt.components.acegi.domain.Role;
import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.util.StringTools;
import ywpt.core.web.BaseAction;
import ywpt.service.system.FunctionService;
import ywpt.service.system.UserService;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class FunctionAction extends BaseAction implements ModelDriven<Function>, Preparable {
	private static final long	serialVersionUID	= 3208248353516560366L;
	protected final Log			log					= LogFactory.getLog(FunctionAction.class);
	private Function			function;
	private FunctionService		functionService;
	private UserService			userService;
	private String				id;
	private List<Function>		functionList		= new ArrayList<Function>();
	private List<Long>			selectedRoles		= new ArrayList<Long>();

	public FunctionAction() {
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Function getFunction() {
		return function;
	}

	public void setFunction(Function function) {
		this.function = function;
	}

	public List<Function> getFunctionList() {
		return functionList;
	}

	public void setFunctionList(List<Function> functionList) {
		this.functionList = functionList;
	}

	public List<Long> getSelectedRoles() {
		return selectedRoles;
	}

	public void setSelectedRoles(List<Long> selectedRoles) {
		this.selectedRoles = selectedRoles;
	}

	public void setFunctionService(FunctionService functionService) {
		this.functionService = functionService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@Override
	@SuppressWarnings("unchecked")
	public String execute() {
	/*	System.out.println("id ="+id+" "+ "can reach functionaction.execute!!!");*/
		JSONArray jsonArray = null;

		HttpServletResponse response = this.getResponse();
		response.setHeader( "Content-Type", "text/html; charset=GBK");

		PrintWriter out = null;
		List roles = new ArrayList();
		/*System.out.println("id ="+id+" "+ "can reach functionaction.execute333!!!");*/
		for (Iterator itr = userService.getUserByUsername(this.getCurrentUserName()).getRoles().iterator(); itr.hasNext();) {
			/*System.out.println("id ="+id+" "+ "can reach functionaction.execute444!!!");*/
			Role r = (Role) itr.next();
			roles.add(r);
		}
		/*System.out.println("id ="+id+" "+ "get roles success!!!");*/
		try {
			out = response.getWriter();
			List<Map<String, Object>> children = new ArrayList<Map<String, Object>>();
			if (!StringTools.isNullOrSpace(id)) {
				children.addAll(functionService.getFunctionTree(new Long(id), roles));
				/*System.out.println("id ="+id+" "+ "get getFunctionTree success!!!");*/
			}
			jsonArray = JSONArray.fromObject(children);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		out.write(WebUtils.toString(jsonArray));
		/*System.out.println("id ="+id+" "+ " write success!!!");*/
		return NONE;
	}

	
	@SuppressWarnings("unchecked")
	public String funcTree() {
		JSONArray jsonArray = null;
		HttpServletResponse response = this.getResponse();
		response.setHeader("Content-Type", "text/html; charset=GBK");
		PrintWriter out = null;
		List roles = new ArrayList();
		for (Iterator itr = userService.getUserByUsername(this.getCurrentUserName()).getRoles().iterator(); itr.hasNext();) {
			Role r = (Role) itr.next();
			roles.add(r);
		}
		try {
			out = response.getWriter();
			List<Map<String, Object>> children = new ArrayList<Map<String, Object>>();
			children.addAll(functionService.getFunctionTree(1L));
			jsonArray = JSONArray.fromObject(children);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		out.write(WebUtils.toString(jsonArray));
		return NONE;
	}

	/**
	 * �����û�
	 * 
	 * @return
	 */
	public String input() {
		return INPUT;
	}

	public String addRoles() {
		return INPUT;
	}

	public String saveRoles() {
		if (selectedRoles != null && selectedRoles.size() > 0) {
			functionService.addRoles(function, selectedRoles);
		} else {
			this.addActionMessage("��ѡ��Ҫ�����Ȩ��!");
			return INPUT;
		}
		return SUCCESS;
	}

	
	/**
	 * �༭����
	 * 
	 * @return
	 */
	public String edit() {
		return INPUT;
	}

	/**
	 * �鿴����
	 * 
	 * @return
	 */
	public String view() {
		return "view";
	}

	/**
	 * ������¹���
	 * 
	 * @return
	 */
	public String save() {
		try {
			if (id == null) {
				functionService.saveFunction(function);
			} else {
				functionService.updateFunction(function);
			}
			this.addActionMessage("������Ϣ�ѱ���!");
			return INPUT;
		} catch (UniqueConstraintException e) {
			if (e.getMessage().indexOf("���ܱ��") != -1) {
				this.addFieldError("functionCode", "���ܱ���ظ�");
			} else {
				this.addActionError("�������:" + e.getMessage());
			}
			return INPUT;
		}
	}

	public Function getModel() {
		return function;
	}

	public void prepare() throws Exception {
		if (id != null) {
			this.function = functionService.getFunctionById(new Long(id));
		} else {
			this.function = new Function();
		}
	}
}